package org.leumasjaffe.initiativeTracker.view;


import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.leumasjaffe.initiativeTracker.Constants;
import org.leumasjaffe.initiativeTracker.event.ChangePageAction;
import org.leumasjaffe.initiativeTracker.event.ChangeTurnAction;
import org.leumasjaffe.initiativeTracker.event.CharGenAction;
import org.leumasjaffe.initiativeTracker.event.CloseGUIAction;
import org.leumasjaffe.initiativeTracker.event.DamageAction;
import org.leumasjaffe.initiativeTracker.event.DelayTurnAction;
import org.leumasjaffe.initiativeTracker.event.HealingSurgeAction;
import org.leumasjaffe.initiativeTracker.event.KillAction;
import org.leumasjaffe.initiativeTracker.event.KillAllAction;
import org.leumasjaffe.initiativeTracker.event.NullInitAction;
import org.leumasjaffe.initiativeTracker.event.RollInitAction;

public class ControlPanel extends JPanel {
	private static final long serialVersionUID = 5322633098516209582L;


	private static ControlPanel instance;
	
	
	private static final int rollSize;
	private static final int createSize;
	private static final int killSize;
	private static final int damageSize;
	private static final int damageBSize;
	private static final int turnSize;
	private static final int healSize;
	private static final int pageSize;
	
	private static final int createPos;
	private static final int killPos;
	private static final int damageBPos;
	private static final int turnPos;
	private static final int healPos;
	private static final int pagePos;
	private static final int nullPos;
	
	static {
		if (System.getProperty("os.name").contains("Windows")) {
			rollSize = 80;
			createSize = 100;
			killSize = 60;
			damageSize = 55;
			damageBSize = 120;
			turnSize = 120;
			healSize = 70;
			pageSize = 100;
			
			createPos = 90;
			killPos = 215;
			damageBPos = 155;
			turnPos = 280;
			healPos = 530;
			pagePos = 605;
			nullPos = 710;
		}
		else {
			rollSize = 80;
			createSize = 100;
			killSize = 40;
			damageSize = 50;
			damageBSize = 110;
			turnSize = 120;
			healSize = 60;
			pageSize = 80;
			
			createPos = 90;
			killPos = 215;
			damageBPos = 145;
			turnPos = 270;
			healPos = 520;
			pagePos = 585;
			nullPos = 670;
		}
	}
		
	private JButton create;
	private JButton kill;
	private JButton killall;
	private JButton damageB;
	private JButton turn;
	private JButton reroll;
	private JButton surge;
	private JButton delay;
	private JTextField damage = new JTextField("", 3);

	private JButton pageUp;
	private JButton pageDown;

	private JButton nullRoll;

	private JButton destroy;
	
	public static ControlPanel getInstance() {
		if (instance == null)
			instance = new ControlPanel();
		return instance;
	}
	
	private ControlPanel() {}
	
	public void init() {

		setLayout(null);
		
		boolean isDM = Constants.isDM;
		
		this.pageUp = new JButton(new ChangePageAction("Page Up", -1));
		this.pageDown = new JButton(new ChangePageAction("Page Down", 1));
		this.destroy = new JButton(new CloseGUIAction("Shut Down"));
		
		this.pageUp.setBounds(pagePos, 10, pageSize, 20);
		this.pageDown.setBounds(pagePos, 30, pageSize, 20);
		this.destroy.setBounds(nullPos, 30, 100, 20);

		add(this.pageUp);
		add(this.pageDown);
		add(this.destroy);
		
		if (isDM) {
			this.create = new JButton(new CharGenAction("Add Character"));
			this.reroll = new JButton(new RollInitAction("Roll Init"));
			this.killall = new JButton(new KillAllAction("KILLALL"));
			this.nullRoll = new JButton(new NullInitAction("Null Init"));
			this.surge =  new JButton(new HealingSurgeAction("Heal"));
			this.kill = new JButton(new KillAction("Kill"));
			this.damageB = new JButton(new DamageAction("Deal Damage"));
			this.turn = new JButton(new ChangeTurnAction("Turn Shift"));
			this.delay = new JButton(new DelayTurnAction("Delay"));
			
			this.surge.setBounds(healPos, 10, healSize, 20);
			this.kill.setBounds(killPos, 5, killSize, 20);
			this.damage.setBounds(createPos, 30, damageSize, 20);
			this.damageB.setBounds(damageBPos, 30, damageBSize, 20);
			this.reroll.setBounds(5, 5, rollSize, 45);
			this.killall.setBounds(800, 10, 80, 40);
			this.create.setBounds(createPos, 5, createSize,20);
			this.nullRoll.setBounds(nullPos, 10, 100, 20);
			this.delay.setBounds(healPos, 30, healSize, 20);
			this.turn.setBounds(turnPos, 10, turnSize, 40);

			add(this.surge);
			add(this.kill);
			add(this.damage);
			add(this.damageB);
			add(this.turn);	
			add(this.delay);
			add(this.reroll);
			add(this.killall);
			add(this.create);	
			add(this.nullRoll);
		}

		repaint();
	}
	
	public int getDamage() {
		return Integer.parseInt(this.damage.getText());
	}

	public void reset() {
		this.damage.setText("");
	}

}
